package de.baumann.browser.utils

import android.content.Context
import android.content.Intent
import com.qiyukf.unicorn.api.*
import de.baumann.browser.QIYU_APP_KEY
import de.baumann.browser.activitys.H5Activity
import de.baumann.browser.api.net.vo.LoginInfo
import org.json.JSONArray
import org.json.JSONObject

const val TITLE = "在线客服"

class UnicornUtil {
    companion object {

        fun init(context: Context) {
            Unicorn.init(context, QIYU_APP_KEY, options(), GlideImageLoader(context))
        }

        private fun options(): YSFOptions? {
            val options = YSFOptions()
            options.statusBarNotificationConfig = StatusBarNotificationConfig()
            options.onMessageItemClickListener = messageItemClickListener
            return options
        }

        private var messageItemClickListener = OnMessageItemClickListener { context, _ ->
            run {
                val intent = Intent(context, H5Activity::class.java)
                intent.putExtra("isUseTitle", false)
//                val url = String.format(EXCHANGE_URL, getUserId())
//                intent.putExtra("url", url)
                context.startActivity(intent)
            }
        }

        fun openServiceActivity(context: Context, className: String) {
            val source = ConsultSource(className, "","")
            Unicorn.openServiceActivity(context, TITLE, source)
        }

        fun setUserInfo(loginInfo: LoginInfo) {
            val userInfo = YSFUserInfo()
            userInfo.userId = loginInfo.user_id.toString()
            val jsonArray = JSONArray()
            val realName = JSONObject().put("key", "real_name").put("value", loginInfo.loginName)
            val avatar = JSONObject().put("key", "avatar").put("value", loginInfo.headUrl)
            jsonArray.put(realName)
            jsonArray.put(avatar)
            userInfo.data = jsonArray.toString()
            Unicorn.setUserInfo(userInfo)
        }
    }

}